﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;

namespace SimpleTerminal.Controls
{
    [ToolboxItem(true)]
    public class TextComboBox : BaseComboBox
    {

        protected string selectedText;

        [Browsable(true)]
        [Description("当文本改变时触发此事件")]
        [Category("自定义事件")]
        public event EventHandler SelectedTextChanged;

        [Browsable(true)]
        [Description("当前选中的文本")]
        [Category("自定义属性")]
        public string SelectedText
        {
            get
            {
                return selectedText;
            }
            set
            {
                selectedText = value;
                selectedIndex = Items.IndexOf(value);
                OnTextChanged(EventArgs.Empty);
                Invalidate();
            }
        }
        [Category("自定义属性")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
        [Localizable(true)]
        [MergableProperty(false)]
        [Browsable(true)]
        public new ObjectCollection<object> Items
        {
            get { return base.Items; }
            private set { base.Items = value; }
        }

        protected override void OnSelectedIndexChanged(EventArgs eventArgs)
        {
            base.OnSelectedIndexChanged(eventArgs);
            SelectedText = Items[SelectedIndex] as string;
        }

        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
            SelectedTextChanged?.Invoke(this, e);
        }

        /// <summary>
        /// 绘制下拉框选项
        /// </summary>
        /// <param name="eventArgs"></param>
        protected override void OnDrawItem(DrawItemEventArgs eventArgs)
        {
            base.OnDrawItem(eventArgs);
            Graphics graphics = eventArgs.Graphics;
            int index = eventArgs.Index;
            Rectangle bounds = eventArgs.Bounds;

            if (index == SelectedIndex)
            {
                using (SolidBrush solidBrush = new SolidBrush(SystemColors.HotTrack))
                {
                    graphics.FillRectangle(solidBrush, bounds);
                }
            }
            else if (eventArgs.Hovered)
            {
                using (SolidBrush solidBrush = new SolidBrush(Color.FromArgb(235, 235, 235)))
                {
                    graphics.FillRectangle(solidBrush, bounds);
                }
            }


            using (SolidBrush brush = new SolidBrush(index == SelectedIndex ? Color.White : ForeColor))
            {
                string vlaue = Items[index] as string;
                Rectangle textRectangle = new Rectangle(
                    bounds.X + Padding.Left,
                    bounds.Y,
                    bounds.Width - Padding.Left - Padding.Right,
                    bounds.Height);

                graphics.DrawString(vlaue, Font, brush, textRectangle, stringFormat);
            }

        }


        /// <summary>
        /// 绘制下拉框主体内容
        /// </summary>
        /// <param name="eventArgs"></param>
        protected override void OnDrawContent(DrawComboBoxContentEventArgs eventArgs)
        {
            using (Pen pen = new Pen(Color.Black, 1))
            using (SolidBrush brush = new SolidBrush(ForeColor))
            {
                eventArgs.Graphics.DrawString(SelectedText,
                    Font, brush, eventArgs.Bounds, stringFormat);
            }
        }
    }
}
